package com.itbaizhan.clouddemoorder.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.clouddemoorder.domain.Order;
import com.itbaizhan.clouddemoorder.dto.OrderDto;
import com.itbaizhan.clouddemoorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 跳转到查询页面并查询订单数据
     * @param model
     * @return
     */
    @GetMapping("/index")
    public ModelAndView selectOrder(Model model) {
        OrderDto orderDto = new OrderDto();
        //获取认证对象,从中拿到token
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)authentication.getDetails();
        Page<Order> orderPage = orderService.selectOrder(orderDto);
        ModelAndView modelAndView = new ModelAndView("order");  // 跳转到 order.html页面
        modelAndView.addObject("orderList", orderPage.getRecords());
        modelAndView.addObject("token", details.getTokenValue()); //获取令牌
        modelAndView.addObject("total", orderPage.getTotal());
        return modelAndView;
    }





}
